#!/bin/sh
G_ML="/tmp/Adbyby"  #安装目录
G_RZ="/tmp/Adbyby/adbyby.log"   #日志位置
G_WZ="/bin/0ad_byby_sh.sh"   #守护脚本位置
G_PID="$G_ML/adbyby.pid"
sleep 1
cat "$G_PID" | xargs kill -s 9
echo $$ > "$G_PID"
echo $(date +"%Y-%m-%d %H:%M:%S")    "守护脚本PID【"$$"】" >> "$G_RZ"
G_JG=10
G_CS=720
G_LA=389900
G_LAP="http://update.adbyby.com/rule3/lazy.jpg"
G_VI=790
G_VIP="http://update.adbyby.com/rule3/video.jpg"
G_US=290
G_USP="http://update.adbyby.com/rule3/"
G_IP=180.76.76.76
G_UP="update.adbyby.com"
G_WG="/tmp/0ad_byby.update"
G_BL=0
while [ "1" ];
do
sleep $G_JG
let G_BL=$G_BL+1
 if [ "$G_BL" -ge "$G_CS" ];then
G_BLL=0
G_JGG=5
while [ "$G_BLL" -eq 0 ] ;
do
sleep $G_JGG
if ping -w 1 -c 1 "$G_IP"
then
if ping -w 1 -c 1 "$G_UP"
then
G_BLL=1
else
echo -e "\n"$(date +"%Y-%m-%d %H:%M:%S")         "【警告】无法连接Adbyby服务器！本轮不更新Adbyby……" >> "$G_RZ"
G_BLL=2
fi
else
if [ "$G_JGG" -eq 5 ];then
killall -s 9 adbyby
iptables -t nat -D PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8118
echo $(date +"%Y-%m-%d %H:%M:%S")         "【错误】无法连接互联网！已自动监控网络。已关闭Adbyby，恢复恢复网络后更新、重启Adbyby……" >> "$G_RZ"
fi
G_BLL=0
G_JGG=59
fi
done
killall -s 9 adbyby
iptables -t nat -D PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8118
 if [ "$G_BLL" -ne 2 ];then
rm -rf "$G_WG"
wget -qO "$G_WG" "$G_LAP"
if [ $(ls -l "$G_WG" | awk '{ print $5 }') -gt $G_LA ]
then
rm -rf "$G_ML"/data/lazy.txt
mv "$G_WG" "$G_ML"/data/lazy.txt
rm -rf "$G_WG"
echo -e "\n"$(date +"%Y-%m-%d %H:%M:%S")    "【lazy.txt】  更新成功。" >> "$G_RZ"
else
rm -rf "$G_WG" 
echo -e "\n"$(date +"%Y-%m-%d %H:%M:%S")    "【更新失败】lazy.txt 更新失败！" >> "$G_RZ"
fi
wget -qO "$G_WG" "$G_VIP"
if [ $(ls -l "$G_WG" | awk '{ print $5 }') -gt $G_VI ]
then
rm -rf "$G_ML"/data/video.txt
mv "$G_WG" "$G_ML"/data/video.txt
rm -rf "$G_WG"
echo $(date +"%Y-%m-%d %H:%M:%S")    "【video.txt】  更新成功。" >> "$G_RZ"
else
rm -rf "$G_WG" 
echo $(date +"%Y-%m-%d %H:%M:%S")    "【错误】video.txt 更新失败！" >> "$G_RZ"
fi
wget -qO "$G_WG" "$G_USP"user.action
if [ $(ls -l "$G_WG" | awk '{ print $5 }') -gt $G_US ]
then
rm -rf "$G_ML"/user.action
mv "$G_WG" "$G_ML"/user.action
rm -rf "$G_WG" "$G_ML"/data/adclear.ini "$G_ML"/data/clean.ini
wget -qO "$G_ML"/data/clean.ini "$G_USP"clean.ini
wget -qO "$G_ML"/data/adclear.ini "$G_USP"adclear.ini
echo $(date +"%Y-%m-%d %H:%M:%S")    "【user.action、clean.ini、adclear.ini】  更新成功。" >> "$G_RZ"
else
rm -rf "$G_WG" 
echo $(date +"%Y-%m-%d %H:%M:%S")    "【错误】user.action、clean.ini、adclear.ini 更新失败！" >> "$G_RZ"
fi
fi
"$G_ML"/adbyby& > /dev/null
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8118
echo $(date +"%Y-%m-%d %H:%M:%S")  "守护脚本运行满【"$G_BL"】次，重启Adbyby成功。" >> "$G_RZ"
G_BL=0
sh "$G_WZ"&
echo $(date +"%Y-%m-%d %H:%M:%S")        "守护脚本【自动重启】成功！" >> "$G_RZ"
exit 0
fi
G_ZF=`ps | grep adbyby | wc -l`
 if [ "$G_ZF" -ne 3 ];then
    killall -s 9 adbyby
    iptables -t nat -D PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8118
    iptables -t nat -D PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8118
    "$G_ML"/adbyby& > /dev/null
    iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8118
    echo $(date +"%Y-%m-%d %H:%M:%S")    "【错误】 守护脚本运行到第【"$G_BL"】次时，发现Adbyby 崩溃！守护脚本已重新启动Adbyby ！" >> "$G_RZ"
 fi
done